package com.snap.awesomeserial.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.snap.awesomeserial.R;
import com.snap.awesomeserial.bean.NAStep;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StepAdapter extends RecyclerView.Adapter<StepAdapter.Holder> {

    private Context context;
    private List<NAStep> steps;

    public StepAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.step_item, parent, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        NAStep step = steps.get(position);
        holder.indexTv.setText(String.format(Locale.CHINA, "%d", step.getIndex()));
        holder.magnetTimeTv.setText(step.getMagnetTime()==-1?"-":String.format(Locale.CHINA, "%ds", step.getMagnetTime()));
        holder.mixtureSpeedTv.setText(String.format(Locale.CHINA, "%d", step.getMixtureSpeed()));
        holder.nameTv.setText(String.format(Locale.CHINA, "%s", step.getName()));
        double f = step.getTemperature();
        DecimalFormat df = new DecimalFormat("#.00");
        holder.temperatureTv.setText(step.getTemperature()==-1?"-":String.format(Locale.CHINA, "%s°C",df.format(f)));
        holder.volumeTv.setText(step.getVolume()==-1?"-":String.format(Locale.CHINA, "%dul", step.getVolume()));
        holder.waitTimeTv.setText(step.getWaitTime()==-1?"-":String.format(Locale.CHINA, "%dmin", step.getWaitTime()));
        holder.mixtureTimeTv.setText(step.getMixtureTime()==-1?"-":String.format(Locale.CHINA, "%dmin", step.getMixtureTime()));
        holder.checkBox.setChecked(step.isChoose());
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                step.setChoose(isChecked);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (steps == null) {
            return 0;
        }
        return steps.size();
    }

    public void bindDataList(List<NAStep> steps) {
        this.steps = steps;
        notifyDataSetChanged();
    }

    public boolean hasChecked() {
        for (NAStep step : steps) {
            if (step.isChoose()){
                return true;
            }
        }
        return false;
    }


    static class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.index_tv)
        TextView indexTv;
        @BindView(R.id.name_tv)
        TextView nameTv;
        @BindView(R.id.wait_time_tv)
        TextView waitTimeTv;
        @BindView(R.id.mixture_time_tv)
        TextView mixtureTimeTv;
        @BindView(R.id.mixture_speed_tv)
        TextView mixtureSpeedTv;
        @BindView(R.id.magnet_time_tv)
        TextView magnetTimeTv;
        @BindView(R.id.volume_tv)
        TextView volumeTv;
        @BindView(R.id.temperature_tv)
        TextView temperatureTv;
        @BindView(R.id.checkbox)
        CheckBox checkBox;

        Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
